const apiStatus = require('../../apiStatus');
const dataService = require('./dataService');

const slideshowsService = require('./slideshowsService');

function setup(app, appRouter){

    appRouter.post('/cms/slideshows', slideshowsService.slideshows);
    appRouter.post('/cms/slideshows/add', slideshowsService.addSlideshow);
    appRouter.post('/cms/slideshows/update', slideshowsService.updateSlideshow);
    appRouter.post('/cms/slideshows/remove', slideshowsService.removeSlideshow);

    appRouter.get('/cms/slideshows/categories', slideshowsService.categories);
    appRouter.post('/cms/slideshows/categories/update', slideshowsService.updateCategory);
    appRouter.post('/cms/slideshows/categories/remove', slideshowsService.removeCategory);


    appRouter.post('/api/data/slideShow', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let categoryId = ctx.request.body.categoryId;
        let slideShows = await dataService.slideShow(appId,categoryId);
        ctx.apiBody = slideShows;
        next();
    });

    appRouter.post('/api/data/richContent', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let richId = ctx.request.body.richId;
        let content = await dataService.richContent(appId,richId);
        ctx.apiBody = content;
        next();
    });

    appRouter.post('/api/data/modelData', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let modelId = ctx.request.body.modelId;
        let count = ctx.request.body.count || 10;
        let model = await dataService.modelData(appId,modelId,count);
        if (model == null) {
          ctx.apiStatus = {apiStatus: apiStatus.serverError};
          return;
        }
        ctx.apiBody = model;
        next();
    });

    appRouter.post('/api/data/navigationSection', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let pageId = ctx.request.body.pageId;
        let model = await dataService.navigationSection(appId,pageId);
        if (model == null) {
          ctx.apiStatus = {apiStatus: apiStatus.serverError};
          return;
        }
        ctx.apiBody = model;
        next();
    });

    appRouter.post('/api/data/formKey', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let formId = ctx.request.body.formId;
        let model = await dataService.formKey(appId,formId);
        if (model == null) {
          ctx.apiStatus = {apiStatus: apiStatus.serverError};
          return;
        }
        ctx.apiBody = model;
        next();
    });

    appRouter.post('/api/data/saveForm', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let formId = ctx.request.body.formId;
        let json = ctx.request.body.json;
        let ret = await dataService.saveFormData(appId,formId,JSON.stringify(json));
        if (ret == false) {
          ctx.apiStatus = {apiStatus: apiStatus.serverError};
          return;
        }
        ctx.apiBody = {apiStatus: apiStatus.success};
        next();
    });
}

module.exports = setup;
